package com.earthea.chatbot.controller;

import com.earthea.chatbot.common.entity.BaseRsp;
import com.earthea.chatbot.entity.domain.Chat;
import com.earthea.chatbot.entity.request.AddChatReq;
import com.earthea.chatbot.entity.request.UpdateChatReq;
import com.earthea.chatbot.entity.response.AddChatRsp;
import com.earthea.chatbot.entity.response.ChatRsp;
import com.earthea.chatbot.login.common.util.SecurityUtils;
import com.earthea.chatbot.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/chat")
public class ChatController {
    @Autowired
    private ChatService chatService;

    @PostMapping
    public AddChatRsp insert(@RequestBody @Validated AddChatReq req) {
        Chat chat = new Chat();
        chat.setUserId(SecurityUtils.getLoginUserId());
        chat.setTitle(req.getTitle());
        return chatService.insert(chat);
    }

    @PutMapping
    public BaseRsp update(@RequestBody @Validated UpdateChatReq updateChatReq) {
        Chat chat = new Chat();
        chat.setChatId(updateChatReq.getChatId());
        chat.setUserId(SecurityUtils.getLoginUserId());
        chat.setTitle(updateChatReq.getTitle());
        return chatService.update(chat);
    }

    @DeleteMapping("/{chatId}")
    public BaseRsp delete(@PathVariable String chatId) {
        Chat chat = new Chat();
        chat.setChatId(chatId);
        chat.setUserId(SecurityUtils.getLoginUserId());
        return chatService.delete(chat);
    }

    @GetMapping
    public ChatRsp selectAll() {
        return chatService.select(SecurityUtils.getLoginUserId());
    }
}
